package com.ruitu.mall_98.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.beanu.arad.Arad;
import com.beanu.arad.base.my_base.Config;
import com.beanu.arad.base.my_base.MyBaseActivity;
import com.beanu.arad.base.my_base.base_list.ListBaseAdapter;
import com.beanu.arad.base.my_base.base_list.SuperViewHolder;
import com.beanu.arad.widget.progressactivity.ProgressLayout;
import com.ruitu.mall_98.R;
import com.ruitu.router_module.AppHolder;
import com.ruitu.router_module.bean.EventModel;
import com.ruitu.router_module.bean.app.Area;

import java.util.ArrayList;
import java.util.List;

//城市列表,选择切换城市
public class CityListActivity extends MyBaseActivity {
    private RecyclerView rcv_list;

    private List<Area> cityList;//城市列表
    private CityAdapter adapter;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_city_list;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rcv_list = findViewById(R.id.rcv_list);
        setHeadTitle("已开通城市");
        cityList = (ArrayList<Area>) getIntent().getExtras().getSerializable("city_list");
        Area sCity = new Area();
        sCity.setName("全国版");
        cityList.add(0, sCity);

        adapter = new CityAdapter(this);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rcv_list.setLayoutManager(manager);
        rcv_list.setAdapter(adapter);
        adapter.setDataList(cityList);

        adapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                AppHolder.instance().setCITY(cityList.get(position));
                Arad.preferences.putString(Config.SAVE_CITY_KEY, cityList.get(position).getName()).flush();
                Arad.bus.post(new EventModel(18111251));
                finish();
            }
        });
    }

    private class CityAdapter extends ListBaseAdapter<Area> {

        public CityAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.app_item_city_list;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            TextView tv_name = holder.getView(R.id.tv_name);
            tv_name.setText(cityList.get(position).getName());
        }
    }

    protected ProgressLayout findProgressLayout() {
        return null;
    }
}
